﻿using System;
using System.ServiceModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnterpriseServices.DalEf;

namespace EnterpriseServices.Service
{
    public class RoleServiceBase : IRoleManagerBase
    {
                /// <summary>
        /// Adds the specified userNames to the specified roleName.
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <param name="userNames">The userName names.</param>
        /// <param name="roleName">The roleName.</param>
        public void AddUsersToRole(string applicationName, string[] userNames, string roleName)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Adds the specified userNames to the specified roleNames.
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <param name="userNames">The userName names.</param>
        /// <param name="roleNames">The roleNames.</param>
        public void AddUsersToRoles(string applicationName, string[] userNames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Adds the specified userName to the specified roleName.
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <param name="userName">Name of the userName.</param>
        /// <param name="roleName">The roleName.</param>
        public void AddUserToRole(string applicationName, string userName, string roleName)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Adds the specified userName to the specified roleNames.
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <param name="userName">Name of the userName.</param>
        /// <param name="roleNames">The roleNames.</param>
        public void AddUserToRoles(string applicationName, string userName, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Adds a new roleName to the data source.
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <param name="roleName">The roleName.</param>
        public void CreateRole(string applicationName, string roleName)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Removes a roleName from the data source. If throwOnPopulatedRole is true,throws an exception if roleName has one or more members.
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <param name="roleName">The roleName.</param>
        /// <param name="throwOnPopulatedRole">if set to <c>true</c> [throw on populated roleName].</param>
        /// <returns></returns>
        public bool DeleteRole(string applicationName, string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Removes all the roleNames from the data source. If throwOnPopulatedRole is true,throws an exception if any of the roleNames has one or more members
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <param name="throwOnPopulatedRole">if set to <c>true</c> [throw on populated roleName].</param>
        public void DeleteAllRoles(string applicationName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets a list of all the roleNames for the applicationName.
        /// </summary>
        /// <param name="applicationName"></param>
        /// <returns></returns>
        public List<EnterpriseServices.DalEf.Role> GetAllRoles(string applicationName)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets a list of the roleNames that a userName is in.
        /// </summary>
        /// <param name="applicationName"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public List<EnterpriseServices.DalEf.Role> GetRolesByUser(string applicationName, string UserName)
        {
                using (ESDBEntities _esdb = new ESDBEntities())
                    return _esdb.Roles_SelectRolesByUser(applicationName, UserName).ToList();
        }

        /// <summary>
        /// Gets a list of userNames in the specified roleName.
        /// </summary>
        /// <param name="applicationName"></param>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public List<EnterpriseServices.DalEf.User> GetUsersInRole(string applicationName, string roleName)
        {
            using (ESDBEntities _esdb = new ESDBEntities())
            {
                Role role = (from r in _esdb.Roles.Include("RoleGroups.Group.GroupUsers.Users")
                             where r.RoleName == roleName
                             where r.Applications.ApplicationName == applicationName
                             select r).FirstOrDefault();
                
                List<User> retval = new List<User>();
                role.RoleGroups.ToList().ForEach(rg => rg.Group.GroupUsers.ToList().ForEach(gu => retval.Add(gu.Users)));
                return retval;                
            }
        }

        /// <summary>
        /// Removes the specified userName from the specified roleName.
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <param name="userName">Name of the userName.</param>
        /// <param name="roleName">Name of the roleName.</param>
        public void RemoveUserFromRole(string applicationName, string userName, string roleName)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Removes the specified userNames from the specified roleName.
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <param name="userNames">The userNames.</param>
        /// <param name="roleName">The roleName.</param>
        public void RemoveUsersFromRole(string applicationName, string[] userNames, string roleName)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Removes the specified userName from the specified roleNames.
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <param name="userName">The userName.</param>
        /// <param name="roleNames">The roleNames.</param>
        public void RemoveUserFromRoles(string applicationName, string userName, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Removes the specified userName names from the specified roleNames.
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <param name="userNames">The userNames.</param>
        /// <param name="roleNames">The roleNames.</param>
        public void RemoveUsersFromRoles(string applicationName, string[] userNames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Returns true if roleName management is enabled; otherwise,false
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <returns>
        /// 	<c>true</c> if [is roleNames enabled] [the specified applicationName]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsRolesEnabled(string applicationName)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Returns true if the roleName name already exists in the data source; otherwise,false.
        /// </summary>
        /// <param name="applicationName">The applicationName.</param>
        /// <param name="roleName">The roleName.</param>
        /// <returns></returns>
        public bool RoleExists(string applicationName, string roleName)
        {
            throw new NotImplementedException();
        }

    }
}
